package vip.xiaonuo.issued.controller.iot;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.issued.bean.CMD;
import vip.xiaonuo.issued.bean.Topic;
import vip.xiaonuo.issued.config.MqttConfiguration;
import vip.xiaonuo.issued.controller.iot.vo.DeviceIdAndTypeVo;
import vip.xiaonuo.issued.controller.iot.vo.GetDeviceOperateFileParam;
import vip.xiaonuo.issued.inter.MqttGateway;
import vip.xiaonuo.issued.util.SnowIdGen;

import java.text.MessageFormat;
import java.util.Map;

@RestController
@Api(value = "设备接口文档")
public class DeviceIotApi {


  @Resource
  private DeviceBaseService deviceBaseService;

  @Autowired
  private MqttConfiguration mqttConfiguration;

  @Resource
  private MqttGateway gateway;

  @ApiOperation(value = "通过序列化换取设备id", tags = "门禁设备-通过序列化换取设备id")
  @ApiImplicitParams({@ApiImplicitParam(name = "serialNumber", value = "设备序列号", dataType = "java.lang.String")})
  @PostMapping("/deviceIot/getDeviceIdBySerialNumber")
  public ResponseData getDeviceIdBySerialNumber(@RequestParam(value = "serialNumber", required = true) String serialNumber) {

    DeviceBase one = deviceBaseService.getOne(new LambdaQueryWrapper<DeviceBase>().eq(DeviceBase::getRegistrationCode, serialNumber));

    if (one == null) {
      throw new ServiceException(404, "不存在该设备！");
    }
    DeviceIdAndTypeVo deviceIdAndTypeVo = new DeviceIdAndTypeVo().setDeviceId(one.getId()).setDeviceType("mqtt")
            .setUris(mqttConfiguration.getUris()).setUsername(mqttConfiguration.getUsername()).setPassword(mqttConfiguration.getPassword());

    return ResponseData.success(deviceIdAndTypeVo);
  }


  @ApiOperation(value = "发送获取操作日志消息", tags = "发送获取操作日志消息")
  @ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备ID", dataType = "java.lang.Long")})
  @PostMapping("/deviceIot/getDeviceOperateFile")
  public ResponseData getDeviceOperateFile(@RequestBody GetDeviceOperateFileParam param) {
    DeviceBase one = deviceBaseService.getById(param.getDeviceId());

    if (one == null) {
      throw new ServiceException(404, "不存在该设备！");
    }
    Map<String, Object> baseMap = SnowIdGen.getBaseMap();
    baseMap.put("deviceId", param.getDeviceId());
    baseMap.put(CMD.CMD, CMD.UPLOAD_LOG);
    String req = JSON.toJSONString(baseMap);
    gateway.sendToMqtt(MessageFormat.format(Topic.DEVICE_TOPIC, param.getDeviceId()), 1, req);
    return ResponseData.success();
  }


}
